/*
联运API接口17
*/
<template>
  <div class="app-page">
    <!-- 操作菜单区 -->
    <my-header>
      <el-button type="primary" @click="handleAddUpdate()" v-repeatClick>添加</el-button>
      <el-button @click="handleBeforeUpdate" v-repeatClick>修改</el-button>
      <el-button @click="enabled(false)">禁用</el-button>
      <el-button @click="enabled(true)">启用</el-button>
      <el-button @click="view" v-repeatClick>查看</el-button>
      <el-button @click="getCompanyAPI" v-repeatClick>刷新</el-button>
      <el-button type="danger" @click="delCompanyAPI" v-repeatClick>删除</el-button>
    </my-header>

    <!-- 列表区 -->
    <el-card  style="flex: 1;">
      <div class="app-page">
        <div class="content-flex">
          <div>
            <el-form ref="search" :model="search" inline size="small">
              <el-form-item label="" prop="info">
                <el-input
                  placeholder="搜索:API名称/备注"
                  v-model="search.info"
                ></el-input>
              </el-form-item>
              <el-form-item label="" prop="companyType">
                <el-select
                  clearable
                  v-model="search.companyType"
                  placeholder="联运快递公司"
                  @change="handleCompanyCodeChange(search.companyType,true)"
                  style="width: 200px"
                >
                  <el-option
                    v-for="item in baseFormOptions.companyName"
                    :key="item.id"
                    :value="item.id"
                    :label="item.companyName + '('+ item.companyType + item.companyCode +')'"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="" prop="enabled">
                <el-select
                  clearable
                  v-model="search.enabled"
                  style="width: 100px"
                  placeholder="是否启用">
                  <el-option label="是" :value="true"></el-option>
                  <el-option label="否" :value="false"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="" prop="">
                <el-button type="primary" round @click="getCompanyAPI">搜索</el-button>
                <el-button
                  type="warning"
                  round
                  @click="$refs.search.resetFields()"
                >重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <!-- 列表区 -->
        <div style="flex: 1">
          <el-table
            height="100%"
            :data="data"
            border
            stripe
            highlight-current-row
            ref="multipleTable"
            @row-dblclick="handleRowDbclick"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="index" width="55" align="center">
            </el-table-column>
            <el-table-column type="selection" width="55" align="center">
            </el-table-column>
            <el-table-column label="API名称" prop="apiName">
              <template slot-scope="scope">
                <el-link @click="handleRowDbclick(scope.row)">{{ scope.row.apiName }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="联运快递公司名称" prop="companyName"  align="center">
            </el-table-column>
            <el-table-column label="账户ID" prop="appId"  align="center">
            </el-table-column>
            <el-table-column label="备注" prop="note"  align="center"></el-table-column>
            <el-table-column label="启用" prop="enabled" align="center" sortable>
              <template slot-scope="scope">
                <span style="font-size: 20px">
                  <i
                    class="el-icon-circle-check"
                    style="color: #67c23a"
                    v-if="scope.row.enabled"
                  ></i>
                  <i
                    class="el-icon-circle-close"
                    style="color: #f56c6c"
                    v-else
                  ></i>
                </span>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" sortable prop="createTime" align="center">
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页区 -->
        <div style="margin-top: 10px">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 50, 100, 200, 500, 1000]"
            :page-size="pageSize"
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </el-card>
    <!-- 添加联运接口API信息 -->
    <el-dialog
      v-dialog-drag
      :title="title + '联运接口API'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @close="restForm"
      width="1000px"
    >
      <el-form
        size="small"
        :model="baseForm"
        label-position="left"
        label-width="120px"
        ref="baseForm"
        :rules="baseFormRules"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="apiName" label="API名称">
              <el-input v-model="baseForm.apiName" placeholder="请输入API名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="companyId" label="联运快递公司">
              <el-select
                ref="select"
                filterable
                style="width: 100%"
                v-model="baseForm.companyId"
                @change="handleCompanyCodeChange"
                placeholder="请选择联运快递公司">
                <el-option
                  v-for="item in baseFormOptions.companyName"
                  :key="item.id"
                  :value="item.id"
                  :label="item.companyName + '('+ item.companyType + item.companyCode +')'"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="appId" label="用户ID标识">
              <el-input v-model="baseForm.appId" placeholder="请输入用户ID标识"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="appKey" label="公匙">
              <el-input v-model="baseForm.appKey" placeholder="请输入公匙"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="appSecret" label="私匙">
              <el-input v-model="baseForm.appSecret" placeholder="请输入私匙"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="appType" label="App类型">
              <el-input v-model="baseForm.appType" placeholder="请输入App类型"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="appFormat" label="格式">
              <el-input v-model="baseForm.appFormat" placeholder="请输入格式"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="appVersion" label="版本">
              <el-input v-model="baseForm.appVersion" placeholder="请输入版本"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item prop="other" label="其他">
              <el-input v-model="baseForm.other" placeholder="其他"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item prop="note" label="备注">
              <el-input v-model="baseForm.note" placeholder="备注" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" style="margin-top:30px">
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-checkbox v-model="baseForm.enabled" style="display: none;">启用</el-checkbox>
              <el-button style="margin-left: 10px" @click="restForm">重置</el-button>
              <el-button type="primary" @click="submit" v-if="title!='查看'">保存</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
let search = {
  //搜索:API名称/备注
  info:'',
  //联运快递公司ID
  companyType:'',
  //联运快递公司名称
  companyName:'',
  //是否启用
  enabled: "",
};
//发货渠道基本信息表单
let baseForm = {
  id:0,
  //API名称
  apiName:'',
  //格式
  appFormat:'',
  //用户ID标识
  appId:'',
  //公匙
  appKey:"",
  //私匙
  appSecret:"",
  //app类型
  appType:"",
  //版本
  appVersion:"",
  //公司ID
  companyId:"",
  //公司名称
  companyName:"",
  //其他
  other:"",
  //备注
  note:"",
  //是否启用
  enabled:true,
};
import myHeader from "@/components/pageHEader/pageHeader";
import { mapGetters } from "vuex";
import {
  getCourierCompanyApi,
  getAllCourierCompanyApi,
  addCourierCompanyApi,
  modCourierCompanyApi,
  delCourierCompanyApi,
  disableOrEnableCourierCompanyApi,
  getCourierCompanyApiById,
} from "@/api/intermodals/api";
import {
  getAllCourierCompany,
} from "@/api/intermodals/express";
export default {
  name: "express",
  components: {
    myHeader,
  },
  data() {
    return {
      pageSize: 20,
      currentPage: 1,
      total: 0,
      data: [],
      //表格选中
      multipleSelection: [],
      //搜索
      search: this.cloneObj(search),
      //对话框
      dialogFormVisible: false,
      title: "添加",
      baseForm:{},
      baseFormOptions:{
        companyName:[]
      },
      baseFormRules:{
        //api名称
        apiName: [this.$RegExp.required("api名称不能为空")],
        //联运快递公司
        companyCode: [this.$RegExp.required("联运快递公司不能为空")],
      },
    };
  },
  computed: {
    ...mapGetters(["stores","roles"]),
  },
  created() {},
  mounted() {
    this.getCompanyAPI();
    this.baseForm = this.cloneObj(baseForm)
    getAllCourierCompany().then(resp=>{
      this.baseFormOptions.companyName = resp.data
    })
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.getCompanyAPI()
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getCompanyAPI()
    },
    //重置表单信息
    restForm() {
      this.$nextTick(() => {
        if (this.$refs.baseForm) this.$refs.baseForm.resetFields();
      });
    },
    handleBeforeUpdate(){
      if (!this.multipleSelection.length) {
        this.tip();
        return;
      }
      if (this.multipleSelection.length > 1) {
        this.tip("只能勾选表格中的一条数据");
        return;
      }
      this.handleAddUpdate(this.multipleSelection[0])
    },
    //添加、修改
    handleAddUpdate(row) {
      //修改处理
      if (row) {
        this.title = "修改";
        getCourierCompanyApiById(row.id).then(resp=>{
          let row = resp.data
          //将数据复制给form
          this.handleModform(row, this.baseForm);
          this.baseForm.id = row.id;
          this.dialogFormVisible = true;
        })
      } else {
        this.baseForm = this.cloneObj(baseForm);
        this.title = "添加";
        this.dialogFormVisible = true;
      }
    },
    //刷新
    getCompanyAPI() {
      let query = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        info: this.search.info,
        companyType:this.search.companyType,
        companyName:this.search.companyName,
        enabled:this.search.enabled,
      };
      getCourierCompanyApi(query).then((resp) => {
        this.data =  resp.data.records
        this.total = resp.data.total;
      });
    },
    //表格双击修改
    handleRowDbclick(row) {
      this.handleAddUpdate(row);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //选择联运公司名称
    handleCompanyCodeChange(val,type){
      this.baseFormOptions.companyName.forEach(item=>{
        if (val==item.id){
          if(type){
            this.search.companyName = item.companyName
            console.log(val,type,'true')
          }else{
            this.baseForm.companyName = item.companyName
          }
        }
      })
    },
    //确定
    submit() {
      this.$refs["baseForm"].validate((valid) => {
        if (valid) {
          let submit = Object.assign(
            this.baseForm
          );
          let request;
          if (this.title == "添加") {
            request = addCourierCompanyApi(submit);
          } else {
            request = modCourierCompanyApi(submit);
          }
          request.then((resp) => {
            this.$message.success(resp.message || "成功");
            this.dialogFormVisible = false;
            this.getCompanyAPI();
          });
        }
      });
    },
    //查看
    view() {
      this.handleBeforeUpdate();
      this.title = '查看'
    },
    //删除
    delCompanyAPI() {
      if (!this.multipleSelection.length) {
        this.tip();
        return;
      }
      this.confirm().then(() => {
        delCourierCompanyApi(this.multipleSelection.map((item) => item.id)).then(
          (resp) => {
            this.$message.success(resp.message || "成功");
            this.getCompanyAPI();
          }
        );
      });
    },
    //启用或禁用
    enabled(enabled) {
      if (!this.multipleSelection.length) {
        this.tip();
        return;
      }
      let ids = this.multipleSelection.map((item) => item.id);
      let tip;
      if (enabled) {
        tip = "此操作将启用联运接口API信息, 是否继续?";
      } else {
        tip = "此操作将禁用联运接口API信息, 是否继续?";
      }
      this.confirm(tip).then(() => {
        disableOrEnableCourierCompanyApi(ids,{enabled}).then((resp) => {
          this.$message.success(resp.message || "成功");
          this.getCompanyAPI();
        });
      });
    },
  },
};
</script>

<style scoped lang='scss'>
.tdItem {
  line-height: 25px;
}
</style>
